//
//  TF_ChapterViewController.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2021/11/27.
//  Copyright © 2021 Talkfun. All rights reserved.
//

import UIKit
typealias ChapterBlock = (_ str: CGFloat ,_ row:Int) -> ()
typealias DelChapterBlock = (_ row:Int) -> ()
class TF_ChapterViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
   
    var datasource:[TFPlaybackChapterPage] = []
    var cellNum:Int!
    var selectedArray:[Int] = []
    var tableview:UITableView!
    var nodataView:UIView!
    @objc public var isShow:Bool = false  //记录是否显示该view
    @objc public var durationBlock: ChapterBlock?
    @objc public var DeselectBlock: DelChapterBlock?
    override func viewDidLoad() {
        super.viewDidLoad()
       
       
        
    }
    
    @objc public func createUI() {
        self.tableview = UITableView.init(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
        self.tableview.delegate = self
        self.tableview.dataSource = self
        self.tableview.backgroundColor = UIColor.clear
        self.tableview.register(TF_ChapterCell.self, forCellReuseIdentifier: "chapter")
        self.tableview.rowHeight = 90
        self.tableview.separatorStyle = UITableViewCell.SeparatorStyle.none
        self.view.addSubview(self.tableview)
        
        self.nodataView = UIView.init()
        self.nodataView.frame = self.view.bounds
        self.view.addSubview(self.nodataView)
       
        
        let noImg = UIImageView.init()
        noImg.frame = CGRect(x: (self.nodataView.frame.size.width-200)/2.0, y: 60, width: 200, height: 150)
        noImg.image = UIImage.init(named: "HTLive.bundle/zhangjieque")
        self.nodataView.addSubview(noImg)
     
        
        let noLabel = UILabel.init()
        noLabel.text = "暂无章节";
        noLabel.font =  UIFont.PingFang(size: 16)
        noLabel.textColor = UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1.0)
        noLabel.textAlignment = .center
        noLabel.frame = CGRect(x: 0, y: 176, width: self.nodataView.frame.size.width, height: 22)
        self.nodataView.addSubview(noLabel)
        
    }
    
    @objc public func updateChapter(list:NSMutableArray?) {
       
      self.datasource.removeAll()
      list?.forEach({  model1 in
          self.datasource.append(model1 as! TFPlaybackChapterPage)
        })
        
        for _ in 0..<self.datasource.count {
            self.selectedArray.append(0)
        }
        
        
        self.tableview.reloadData()
        if self.datasource.count > 0 {
            self.nodataView.isHidden = true
        } else {
            self.nodataView.isHidden = false
        }

    }
    
  
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.datasource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "chapter", for: indexPath) as! TF_ChapterCell
       
        cell.number = indexPath.row
        cell.selectedArray = self.selectedArray
        cell.cellwidth = self.view.frame.size.width
        let model:TFPlaybackChapterPage = self.datasource[indexPath.row]
        cell.setmodel(model: model)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     
     let Model:TFPlaybackChapterPage = self.datasource[indexPath.row];
         let duration :CGFloat = Model.starttime
        if self.durationBlock != nil {

            self.durationBlock?(duration,indexPath.row)
        }

        
    }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        self.selectedArray[indexPath.row] = 0
        if self.DeselectBlock != nil {
            self.DeselectBlock?(indexPath.row)
        }
    }
    
    @objc public func refreshUIWithIndex(num:Int) {
        
        if self.datasource.count == 0 {
            return
        }
        
        
        for index in 0..<self.selectedArray.count {
            
            self.selectedArray[index] = 0
            
        }
        
       if(self.selectedArray.count>num)
         {
            self.selectedArray[num] = 1
            self.tableview.reloadData()
         }
    
        

    }
    
    @objc public func refreshUIWithDuration(duration:CGFloat) {
        
        let i = 50
        var j = 0
        for k in stride(from: 0, to: self.datasource.count, by: 50) {
                
               j = 0
              
            let model:TFPlaybackChapterPage = self.datasource[k]
            if model.starttime - 1 > duration {
                j=k
                break
            }
          
            j = 1
        }
        //第一个数据的starttime就大于duration
        if j == 0 {
            if self.selectedArray.count > 1 {
                if self.selectedArray[1] != 1 {
                    self.cellNum = 1
                    self.refreshUIWithIndex(num: self.cellNum)
                }
            }
           
        }
        //前50 * （i - 1）个数据里面没有一个的starttime比duration大
        else if (j == 1) {
          
            for index in stride(from: i - 50, to: self.datasource.count, by: 1)  {
                let model:TFPlaybackChapterPage = self.datasource[index]
                if model.starttime - 1 > duration {
                    if (self.cellNum != index) {

                        self.refreshUIWithIndex(num: index)
                        self.cellNum = index;
                    }
                    break
                }
           }
            
            if i == self.datasource.count {
                if self.cellNum != i {
                    self.refreshUIWithIndex(num: self.datasource.count)
                    self.cellNum = self.datasource.count
                }
            }
        
        
        }
        //有数据的starttime比duration大 但不是第一个
        else if (j != 0) {
            for t in stride(from:j-50, to: j, by: 1) {
                let model:TFPlaybackChapterPage = self.datasource[t]
                if model.starttime - 1 > duration {
                    if self.cellNum != t {
                        self.refreshUIWithIndex(num: t)
                        self.cellNum = t
                    }
                    break
                }
            }
        }

        
    }
    

   
}
